// Package events
// @Author: zhangdi
// @File: window_change
// @Version: 1.0.0
// @Date: 2023/6/14 11:12
package events

import (
	"github.com/wailsapp/wails/v2/pkg/runtime"
	ctx2 "ideatools/application/core/ctx"
	"ideatools/application/pkgs/mylog"
	"strconv"
)

//踩坑：EventsEmit（） 事件名称不能用常量，前端接收不到通知

type WinType = int

// OnChangeWindow 通知前端，切换窗体
func OnChangeWindow(WindowType WinType) {
	ctx := ctx2.GetCtx()
	if ctx == nil {
		mylog.Error("OnChangeWindow()error:GetCtx() == nil")
		return
	}
	mylog.Debug("开始通知窗体OnChangeWindow()" + strconv.Itoa(WindowType))
	runtime.EventsEmit(ctx, "window_change", WindowType)
}

// OnMessageStream 消息流通知
//func OnMessageStream(data MessageStream) {
//	ctx := ctx2.GetCtx()
//	if ctx == nil {
//		mylog.Error("OnMessageStream()error:GetCtx() == nil")
//		return
//	}
//	mylog.Debug("开始通知窗体OnMessageStream()")
//
//	//runtime.EventsEmit(ctx, constant.EventNameNotifyWindowChange, data)
//	runtime.EventsEmit(ctx, "message_stream", data)
//}

// OnContentAdd 添加一条消息
func OnContentAdd(data *MessageContent) {
	ctx := ctx2.GetCtx()
	if ctx == nil {
		mylog.Error("OnMessageStream()error:GetCtx() == nil")
		return
	}

	//if data.ToolId >= 0 {
	//	ToolInfo := dbservice.NewUserToolService().FindOneById(data.ToolId)
	//	if ToolInfo != nil {
	//		data.PromptTemplateText = ToolInfo.Prompt
	//	}
	//}

	mylog.Debug("开始通知窗体OnMessageStream() 1")

	//runtime.EventsEmit(ctx, constant.EventNameNotifyWindowChange, data)
	runtime.EventsEmit(ctx, "message_add_content", data)
}

func OnNewChat(IsUseSelect bool) {
	ctx := ctx2.GetCtx()
	if ctx == nil {
		mylog.Error("OnAiSendError()error:GetCtx() == nil")
		return
	}
	runtime.EventsEmit(ctx, "new_chat", IsUseSelect)

}

func OnTest() {
	ctx := ctx2.GetCtx()
	if ctx == nil {
		mylog.Error("OnTest()error:GetCtx() == nil")
		return
	}
	mylog.Debug("开始通知窗体OnMessageStream()")
	runtime.EventsEmit(ctx, "event1", "test")
}
